<template>
  <div >
      <view>
        <van-dialog id="van-dialog" />
        <van-toast id="van-toast" />
        <div class="tooslbtns"  style="text-align: right; padding-right: 42rpx; border-bottom: 1px solid #ccc;padding-bottom: 20rpx" >
          <van-button  type="info"  size="small" @tap="addExpress" >提交快递单号</van-button>
        </div>
        <view style='width: 100%;  height: 1rpx' />
        <i-row class="header-row">
          <i-col span="8" i-class="col-class">
            <view class="table_parent">时间</view>
          </i-col>
          <i-col span="15" i-class="col-class">
            <view class="table_parent">快递号</view>
          </i-col>

        </i-row>
        <view class="block-order" style='width: 100%;  height: 1rpx;' />
        <block   v-for="(item, index) in orderlist" :key="index" >
          <i-row class="row-order" @tap="goInfo(item._id)">
            <i-col span="8" i-class="col-class">
              <view class="table_parent">{{item.addtime}}</view>
            </i-col>
            <i-col span="15" i-class="col-class">
              <view class="table_parent">
                <van-cell-group>
                  <van-field
                    placeholder="请输入快递单号"
                    @tap.stop="ExpressFieldtap"
                    @change="ExpressFieldonChange"
                  />
                </van-cell-group>
              </view>
            </i-col>
          </i-row>
        </block>
      </view>




  </div>
</template>

<script>

  import Notify from '../../../static/vant/notify/notify';
  import Dialog from '../../../static/vant/dialog/dialog';
  import Toast from  '../../../static/vant/toast/toast';

  import * as dayjs from 'dayjs'
  import 'dayjs/locale/zh-cn' // import locale


export default {
  data () {
    return {
      orderlist:[],
      chosenContactId: null,
      editingContact: {},
      showList: false,
      showEdit: false,
      isEdit: false,
      values:[]


    }
  },

  mounted() {
      this.rowdata()



  },
    methods: {

    ExpressFieldtap(){

    },

    ExpressFieldonChange(data){
      console.log(data.mp.target.dataset.eventid)
      let a = data.mp.target.dataset.eventid.split('_')
      console.log(222222222)
      console.log(a[1])
      this.values[a[1]].express = data.mp.detail
    },


   addExpress(){

      let t = this
      Dialog.confirm({
        message: '确定提交所有快递单号'
      }).then(() => {
        wx.cloud.init({
          env: t.globalData.env
        })
        Toast.loading({
          message: '提交中...'
        });
        //剔除邮编为空的数据
        let a = t.values.filter(function(val,key) {
          if(val.express){
            return val
          }
        })
        t.values = a
        console.log(a)



        wx.cloud.callFunction({

            name: 'manageOrder',
            data:{
              values:t.values,
              want:"update"
            },


          success(res) {
            if(res>0){
              Toast.success('提交成功！');
              t.rowdata()
            }
          }
        })
      }).catch(() => {
        // on cancel
      });
    },

    rowdata(){
      let t = this
      wx.cloud.init({
        env: t.globalData.env
      })
      wx.cloud.callFunction({
        name: 'manageOrder',
        data:{
          where:{
            express:null
          },
          want:"list"
        },
        success(res) {
          if(res.result){
            let data = res.result.data
            var newArr = data.map(function(item){
              item["addtime"] = dayjs(item["addtime"]*1000).format('MM-DD HH:mm:ss')
              return item
            })
            t.orderlist = newArr
            //得到完整数组后，再获取过滤数组
            console.log(newArr)
            t.values = []
            data.map(function(item){
              t.values.push({
                _id:item._id,
                express:""
              })
            })
          }
        }
      })
    },
    goInfo(d){

      wx.navigateTo({
        url: "/pages/manageOrder_info/main?id="+d
      })
    }
  }
}
</script>

<style >
  ._van-field{
    background: red;
  }

  .cbox .van-col{
    margin:28rpx;
  }

  .tooslbtns .van-button--small{
    margin: 0 11rpx;
  }

  .block-order{
    margin-top: 95rpx;
  }

  .header-row{
    position: fixed;
    width: 100%;
    line-height: 2;
    margin-top: -1rpx;
    border-bottom: 1rpx solid #ccc;
  }


  .row-order{
    line-height: 2;
  }

  .row-order .van-row{
    border-bottom: 1rpx solid #eee;
  }

  page {
    background-color: #fff;
  }

  .table_parent {
    display:flex;
    justify-content:center;
    align-items:center;
    background: white;
    font-size:30rpx;
    text-align:center;
    box-sizing:border-box;
    color:#595a5e;
    padding-top: 20rpx;
    padding-bottom: 20rpx;
  }
  .table {
    border: 1px solid #dadada;
    border-right: 0;
    border-bottom: 0;
    width: 98%;
  }

  .tr {
    width: 100%;
    display: flex;
    justify-content: space-between;
  }

  .th, .td {
    padding: 10px;
    border-bottom: 1px solid #dadada;
    border-right: 1px solid #dadada;
    text-align: center;
    width: 100%;
  }

  .th {
    font-weight: 400;
    background-color: #dadada;
  }

</style>
